package com.capgemini.wechat.service;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.capgemini.commons.sftp.SftpFileDto;

/**
 * 
 * @author
 * @date
 */
@Component
public class FileImportService {
	public final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private SaleOrderFileImportService saleOrderFileImportService;
	@Autowired
	private CustomerFileImportService customerFileImportService;
	@Autowired
	private VehicleFileImportService vehicleFileImportService;


	public void processMARSFile() {
<<<<<<< .mine
//		try {
//			saleOrderFileImportService.downloadSFTPFile();
//		} catch (IOException e) {
//			log.error("", e);
//		} catch (ParseException e) {
//			log.error("", e);
//		} catch (ConfigurationException e) {
//			log.error("", e);
//		}
		List<String> filePath = listFilePaths();
//		for (int j = FileImportBaseService.fileList.size() - 1; j >= 0; j--) {
		for (int j = 0; j < filePath.size(); j++) {
//			SftpFileDto sftpFileDto = FileImportBaseService.fileList.get(j);
//			String localFilePath = saleOrderFileImportService.unzipFile(sftpFileDto);
			String localFilePath = filePath.get(j);
=======
		try {
			saleOrderFileImportService.downloadSFTPFile();
		} catch (Exception e) {
			log.error("", e);
		}
//		List<String> filePath = listFilePaths();
//		for (int j = FileImportBaseService.fileList.size() - 1; j >= 0; j--) {
		for (int j = 0; j < FileImportBaseService.fileList.size(); j++) {
			SftpFileDto sftpFileDto = FileImportBaseService.fileList.get(j);
			String localFilePath = saleOrderFileImportService.unzipFile(sftpFileDto);
//			String localFilePath = filePath.get(j);
>>>>>>> .r212
			boolean passFlg = true;
			log.info("file ==> " + localFilePath + " processing....");
			try {
				saleOrderFileImportService.importSaleOrderCSV(localFilePath);
			} catch (Exception e) {
				passFlg = false;
				if(e instanceof SQLException) {
					log.error("loading file -> " + localFilePath + "/SaleOrder.csv failure");
				} else {
					log.error("", e);
				}
			}
			try {
				customerFileImportService.importCustomerCSV(localFilePath);
			} catch (Exception e) {
				passFlg = false;
				if(e instanceof SQLException){
					log.error("loading file -> " + localFilePath + "/Customer.csv failure");
				} else {
					log.error("", e);
				}
			}
			try {
				vehicleFileImportService.importVehicleCSV(localFilePath);
			} catch (Exception e) {
				passFlg = false;
				if(e instanceof SQLException) {
					log.error("loading file -> " + localFilePath + "/Vehicle.csv failure");
				} else {
					log.error("", e);
				}
			}
			// 将处理过的压缩包删除
//			if(passFlg) {
//				FileImportBaseService.removeFile(sftpFileDto);
//			}
		}

		//预处理订单信息
		new ProcessOrderInfoThread().start();
	}

	public List<String> listFilePaths() {
		File file = new File("D:/temp/ServiceReminder");
		List<String> filePath = new ArrayList<String>();
//		for(File f : file.listFiles()) {
//			if(f.isDirectory()) {
//				log.info(f.getAbsolutePath());
//				filePath.add(f.getAbsolutePath());
//			}
//		}
//		Collections.sort(filePath);
//		filePath = filePath.subList(107, filePath.size());
		filePath.add("D:/temp/ServiceReminder/ServiceReminder20160804_143802148");
		filePath.add("D:/temp/ServiceReminder/ServiceReminder20160804_144538575");
		return filePath;
	}
}
